<template>
    <div>
        <a-modal v-model="show" :title="form.id?'编辑':'新增'" @ok="handleOk" @cancel="show = false">
            <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 19 }">
                <a-form-item label="手机号">
                    <a-input v-model="form.mobile" />
                </a-form-item>
                <a-form-item label="用户名称">
                    <a-input v-model="form.userName" />
                </a-form-item>
                <a-form-item label="适用组织">
                    <a-select v-model="form.orgId" placeholder="选择组织">
                        <a-select-option :value="item.orgId"
                            v-for="(item,index) in [{orgId:'',orgName:'选择组织'},...orgList]" :key="index">
                            {{item.orgName}}</a-select-option>
                    </a-select>
                </a-form-item>

            </a-form>
        </a-modal>
    </div>
</template>
<script>
    import { message } from 'ant-design-vue';
    import { postUserAdd, postUserUpdate } from "@/services"
    export default {
        props: {
            // 组织列表
            orgList: {
                default: () => { },
                type: Array,
            },
        },
        data() {
            return {
                // 是否显示弹框
                show: false,
                // 表单
                form: {
                    mobile: '',
                    userName: '',
                    operator: '',
                    orgId: '',
                }
            }
        },
        methods: {
            /**
             * 打开弹框
             */
            open(data) {
                this.show = true // 打开弹框
                this.form = this.$options.data().form // 初始化
                if (data?.userId) {
                    this.form = data
                }

            },
            /**
             * 确认提交
             */
            async handleOk() {
                let { form } = this
                if (form.mobile == '') {
                    return message.warning('请填写手机号')
                }
                if (!/^1[23456789]\d{9}$/.test(form.mobile)) {
                    return message.warning('手机号格式不正确')
                }
                if (form.userName == '') {
                    return message.warning('请填写用户名称')
                }
                if (form.orgId == '') {
                    return message.warning('请选择组织')
                }
                if (form.userId) {
                    let res = await postUserUpdate(this.form)
                    if (res.code == 0) {
                        message.success('修改成功')
                    } else {
                        return message.warning(res?.msg)
                    }
                } else {
                    let res = await postUserAdd(this.form)
                    if (res.code == 0) {
                        message.success('添加成功')
                    } else {
                        return message.warning(res?.msg)
                    }
                }
                this.show = false
                this.$emit('ok', 1);
            }
        },
    }
</script>